package edu.whu.webgis.controller;


import edu.whu.webgis.entity.Message;
import edu.whu.webgis.server.WebSocketServer;
import edu.whu.webgis.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    WebSocketServer webSocketServer;


    @Autowired
    IMessageService messageService;


    @PostMapping("/push")
    public ResponseEntity<String> pushToWeb(@RequestBody Message message){
        webSocketServer.sendInfo(message);
        return ResponseEntity.ok("SUCCESS");
    }

    @GetMapping("/get")
    public ResponseEntity<List<Message>> getMessages(@RequestParam Integer id, @RequestParam Integer limit) {
        return ResponseEntity.ok(messageService.selectMessageLimit(id, limit));
    }

    @GetMapping("/history")
    public ResponseEntity<List<Message>> getHistory(@RequestParam Integer id, @RequestParam Integer limit) {
        return ResponseEntity.ok(messageService.selectHistoryMessage(id, limit));
    }


}
